package com.qianxun.spider.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具，提供对日期的相关操作，如：日期格式化，年月日时分秒的增减操作等
 *
 * @author: huwei
 * @date: 2020/1/2 13:53
 * @version: 1.0.0
 */
public class DateUtil {

    final static public String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    static public Date now() {
        return new Date();
    }

    static public Date date1970() {
        return new Date(0);
    }

    static public Date incrementYear(Date date, int years) {
        return increment(date, years, Calendar.YEAR);
    }

    static public Date decrementYear(Date date, int years) {
        return decrement(date, years, Calendar.YEAR);
    }

    static public Date incrementMonth(Date date, int months) {
        return increment(date, months, Calendar.MONTH);
    }

    static public Date decrementMonth(Date date, int months) {
        return decrement(date, months, Calendar.MONTH);
    }

    static public Date incrementDay(Date date, int days) {
        return increment(date, days, Calendar.DATE);
    }

    static public Date decrementDay(Date date, int days) {
        return decrement(date, days, Calendar.DATE);
    }

    static public Date incrementHour(Date date, int hours) {
        return increment(date, hours, Calendar.HOUR_OF_DAY);
    }

    static public Date decrementHour(Date date, int hours) {
        return decrement(date, hours, Calendar.HOUR_OF_DAY);
    }

    static public Date incrementMinute(Date date, int minutes) {
        return increment(date, minutes, Calendar.MINUTE);
    }

    static public Date decrementMinute(Date date, int minutes) {
        return decrement(date, minutes, Calendar.MINUTE);
    }

    static public Date incrementSecond(Date date, int seconds) {
        return increment(date, seconds, Calendar.SECOND);
    }

    static public Date decrementSecond(Date date, int seconds) {
        return decrement(date, seconds, Calendar.SECOND);
    }

    static public Calendar toCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    static public String format(Date date) {
        return format(date, DEFAULT_DATE_FORMAT);
    }

    static public String format(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    static public Date parse(String dateString) {
        return parse(dateString, DEFAULT_DATE_FORMAT);
    }

    static public Date parse(String dateString, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(dateString);
        } catch (Exception e) {
            throw new RuntimeException("parse date exception ,data :" + dateString + " ,format :" + format);
        }
    }

    static public Date parse(String dateString, Date defaultValue) {
        return parse(dateString, DEFAULT_DATE_FORMAT, defaultValue);
    }

    static public Date parse(String dateString, String format, Date defaultValue) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(dateString);
        } catch (Exception e) {
        }
        return defaultValue;
    }

    static public Date parseDefaultNull(String dateString) {
        return parse(dateString, DEFAULT_DATE_FORMAT, null);
    }

    static public int getYear(Date date) {
        return getTime(date, Calendar.YEAR);
    }

    /**
     * 月份从0开始，所以
     *
     * @param date
     * @return
     */
    static public int getMonthFromZero(Date date) {
        return getTime(date, Calendar.MONTH);
    }

    static public int getMonthFromOne(Date date) {
        return getTime(date, Calendar.MONTH) + 1;
    }

    static public int getDayTime(Date date) {
        return getTime(date, Calendar.DATE);
    }

    static public int getHourTime(Date date) {
        return getTime(date, Calendar.HOUR_OF_DAY);
    }

    static public int getMinutes(Date date) {
        return getTime(date, Calendar.MINUTE);
    }

    static public int getSecond(Date date) {
        return getTime(date, Calendar.SECOND);
    }

    static public int getMillisecond(Date date) {
        return getTime(date, Calendar.MILLISECOND);
    }

    static private int getTime(Date date, int dateType) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(dateType);
    }

    static private Date increment(Date date, int incrementNumber, int dateType) {
        Calendar calendar = toCalendar(date);
        calendar.set(dateType, getTime(date, dateType) + incrementNumber);
        return calendar.getTime();
    }

    static private Date decrement(Date date, int decrementNumber, int dateType) {
        Calendar calendar = toCalendar(date);
        calendar.set(dateType, getTime(date, dateType) - decrementNumber);
        return calendar.getTime();
    }

    /**
     * 今天 最后结束时间
     *
     * @return 今天日期最后时间 23:59:59
     */
    public static Date getTodayEndTime() {
        return getDateEndTime(new Date());
    }

    /**
     * 指定 最后结束时间
     *
     * @return 指定时间 最后时间 23:59:59
     */
    public static Date getDateEndTime(Date date) {
        String dateString = format(date, "yyyy-MM-dd 24:00:01");
        return parse(dateString, "yyyy-MM-dd hh:mm:ss");
    }

    public static Date getTodayStartTime() {
        return getDateStartTime(new Date());
    }

    public static Date getDateStartTime(Date date) {
        String dateString = format(date, "yyyy-MM-dd 00:00:00");
        return parse(dateString, "yyyy-MM-dd hh:mm:ss");
    }


    /**
     * @param endDate 结束时间
     * @param nowDate 当前时间
     * @return 相差分钟数
     */
    public static Long getBetweenMin(Date endDate, Date nowDate) {
        long minTime = 60 * 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少分钟
        long min = diff / minTime;
        return min;
    }

    /**
     * 获取下个月的今天
     *
     * @param currDate
     * @return
     */
    public static Date getNextMonthToDay(Date currDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(currDate);
        c.add(Calendar.MONTH, 1);
        return c.getTime();
    }
}
